docker部署PaddleOCR 您所在的位置:网站首页 paddle ocr java docker部署PaddleOCR

docker部署PaddleOCR

2024-07-13 08:03| 来源: 网络整理| 查看: 265

docker部署PaddleOCR CPU版 1. PaddleOCR 镜像制作2. 运行3. 服务配置4. 测试调用5. 遇到的问题

用 docker 部署 PaddleOCR 是因为 PaddleOCR 以源码安装的方式比较繁杂,要注意比较多的细节,而且很多环境往往是没有外网的,因此Docker就是一个很好的解决方案,它将开放所需要的环境都封装在镜像中了,方便部署使用。

1. PaddleOCR 镜像制作

Dockerfile文件

# Version: 2.5.1 # FROM paddlepaddle/paddle:2.5.1 FROM registry.baidubce.com/paddlepaddle/paddle:2.5.1 # PaddleOCR base on Python3.7 RUN pip3.7 install --no-cache-dir --upgrade pip -i https://mirror.baidu.com/pypi/simple RUN pip3.7 install --no-cache-dir paddlehub --upgrade -i https://mirror.baidu.com/pypi/simple RUN git clone https://gitee.com/PaddlePaddle/PaddleOCR.git /PaddleOCR WORKDIR /PaddleOCR RUN pip3.7 install --no-cache-dir -r requirements.txt -i https://mirror.baidu.com/pypi/simple RUN mkdir -p /PaddleOCR/inference/ # 下载并解压 OCR 文本检测模型 ADD https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar /PaddleOCR/inference/ # 下载并解压 OCR 文本方向分类模型 ADD https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar /PaddleOCR/inference/ # 下载并解压 OCR 文本识别模型 ADD https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar /PaddleOCR/inference/ RUN tar xf /PaddleOCR/inference/ch_PP-OCRv3_det_infer.tar -C /PaddleOCR/inference/ RUN tar xf /PaddleOCR/inference/ch_ppocr_mobile_v2.0_cls_infer.tar -C /PaddleOCR/inference/ RUN tar xf /PaddleOCR/inference/ch_PP-OCRv3_rec_infer.tar -C /PaddleOCR/inference/ RUN pip3.7 uninstall -y astroid RUN pip3.7 install astroid==2.12.2 --upgrade pip -i https://mirror.baidu.com/pypi/simple RUN pip3.7 uninstall protobuf RUN pip3.7 install protobuf==3.20.0 --upgrade pip -i https://mirror.baidu.com/pypi/simple # 安装ocr_system服务 RUN hub install deploy/hubserving/ocr_system/ # 安装表格识别,下载基于SLANet的中文表格识别模型,如果只需要文本提取,则可以不安装该服务,删掉这一块即可 ADD https://paddleocr.bj.bcebos.com/ppstructure/models/slanet/ch_ppstructure_mobile_v2.0_SLANet_infer.tar /PaddleOCR/inference/ ADD https://paddleocr.bj.bcebos.com/dygraph_v2.0/table/en_ppocr_mobile_v2.0_table_structure_infer.tar /PaddleOCR/inference/ RUN tar xf /PaddleOCR/inference/ch_ppstructure_mobile_v2.0_SLANet_infer.tar -C /PaddleOCR/inference/ RUN tar xf /PaddleOCR/inference/en_ppocr_mobile_v2.0_table_structure_infer.tar -C /PaddleOCR/inference/ #安装structure_table服务 RUN hub install deploy/hubserving/structure_table/ EXPOSE 8866 CMD ["/bin/bash","-c","hub serving start -m ocr_system structure_table"]

创建好Dockerfile文件后,执行如下命令即可自动构建镜像,要预留足够的存储空间,构建完成后大概6G多,整个构建过程根据网速定,我花了差不多1.5小时才构建完。

docker build -t paddle-ocr:cpu . 2. 运行 docker run -dp 8866:8866 --name ocr paddle-ocr:cpu

当然也可以用 docker-compose 管理

version: '3' services: ocr: image: paddle-ocr:cpu restart: always container_name: ocr ports: - 8866:8866 3. 服务配置

镜像运行后,使用 docker exec -it ocr bash 进入容器内进行修改,修改后重新容器即可。

文本检测+文本方向分类+文本识别3阶段串联服务(ocr_system)配置文件是deploy/hubserving/ocr_system/params.py,包含模型路径和相关参数,这里使用默认配置即可,如果更换模型需要对应修改配置文件。表格识别服务(structure_table)配置. #打开配置文件 vim deploy/hubserving/structure_table/params.py #调整模型文件路径为./inference/ch_ppstructure_mobile_v2.0_SLANet_infer/ #调整字典文件路径为./ppocr/utils/dict/table_structure_dict_ch.txt 4. 测试调用 org.apache.httpcomponents httpclient 4.5.14 import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.clienthods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import sun.misc.BASE64Encoder; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.text.ParseException; public class HttpUtils { //接口地址 //private static final String apiURL = "http://192.168.0.101:8866/predict/structure_table"; private static final String apiURL = "http://192.168.0.101:8866/predict/ocr_system"; public static void main(String[] args) throws ParseException { JSONArray arry = new JSONArray(); JSONObject param = new JSONObject(); arry.add(fileToBase64("C:\\Users\\Aaron\\Pictures\\2.PNG")); param.put("images",arry); String result = HttpUtils.post(apiURL, param.toJSONString()); System.out.println(result); } /** * 调用 API */ public static String post(String url, String parameters) { HttpPost post = new HttpPost(url); // 建立一个NameValuePair数组,用于存储欲传送的参数 post.addHeader("Content-type","application/json"); post.setHeader("Accept", "application/json"); post.setEntity(new StringEntity(parameters, StandardCharsets.UTF_8)); long startTime = System.currentTimeMillis(); try (CloseableHttpClient httpClient = HttpClients.createDefault()){ HttpResponse response = httpClient.execute(post); long endTime = System.currentTimeMillis(); int statusCode = response.getStatusLine().getStatusCode(); System.out.println("statusCode:" + statusCode); System.out.println("调用API 花费时间(单位:秒):" + (endTime - startTime)/1000); if (statusCode != HttpStatus.SC_OK) { System.out.println("Method failed:" + response.getStatusLine()); } String body = EntityUtils.toString(response.getEntity(),"utf-8"); return body; } catch (IOException e) { // 网络错误 e.printStackTrace(); } return ""; } public static String fileToBase64(String path) { // 读取图片字节数组 try (InputStream in = Files.newInputStream(Paths.get(path))){ byte[] data = new byte[in.available()]; in.read(data); // 对字节数组Base64编码 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data);// 返回Base64编码过的字节数组字符串 } catch (IOException e) { e.printStackTrace(); } return ""; } } 5. 遇到的问题 报 protobuf 包依赖冲突,后在Dockerfile 文件里后面加了这行,安装个低版本的 protobuf 解决 RUN pip install protobuf==3.20.0 -i https://mirror.baidu.com/pypi/simple PaddlePaddle出现“非法指令”或“illegal instruction”

原因:PaddlePaddle使用avx SIMD指令提高cpu执行效率,因此错误的使用二进制发行版可能会导致这种错误,请先判断你的电脑是否支持AVX指令集,再选择性的安装支持AVX指令集的PaddlePaddle还是不支持AVX指令集的PaddlePaddle,或者使用Docker镜像来安装最新版本的PaddlePaddle,Docker镜像中的PaddlePaddle默认支持是支持AVX指令集的,可以提高cpu的执行效率。在启动的时候会检查系统是否支持PaddlePaddle初始化时所需要的资源,从AVX指令集开始检查。 解决:遇到此问题可以先用 lscpu 命令查看docker宿主机是否支持AVX指令集,我是换台服务器解决。

容器运行后,调用的时候报了 module ‘numpy’ has no attribute ‘int’. 异常 AttributeError: module 'numpy' has no attribute 'int'. `np.int` was a deprecated alias for the builtin `int`. To avoid this error in existing code, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information. The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:

原因:np.int 在 NumPy 1.20 中已弃用,在 NumPy 1.24 中已删除。 解决:将容器中的 /PaddleOCR/deploy/hubserving/ocr_system/module.py /np.ini 文件中的 np.int 更改为np.int_

具体操作如下:

# 进入容器内 docker exec -it ocr /bin/bash # 修改文件 vim /PaddleOCR/deploy/hubserving/ocr_system/module.py /np.ini # 如下位置 for dno in range(dt_num): text, score = rec_res[dno] rec_res_final.append({ 'text': text, 'confidence': float(score), 'text_region': dt_boxes[dno].astype(np.int_).tolist() }) all_results.append(rec_res_final) return all_results # 修改后重启容器即可解决 docker restart ocr


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有